/*----------------------------------------------------------------------
  - File name      : init.c
  - Author         : Quark Team
  - Update date    : 2020-12-13                   
  -	Copyright      : Gevico Electronic studio   
  - Module comments: Layout module.
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include "main.h"
#include "app.h"
/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/

/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: MCU peripheral init function             |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void STC8Ax_PER_Init(void)
{
	
	DELAY_POS(); /* Power on stability delay */
	
//--------------------------------------------------------		
	
	STC8Ax_SYSCLK_Config(); /* Initialize system clock */
	delay_init(); /* Initialization delay function */
	
//--------------------------------------------------------

	STC8Ax_GPIO_Config();
	STC8Ax_TIMER_Config();
	STC8Ax_UART_Config();

//--------------------------------------------------------		
	
	/*
		Add hardware driver initialization code here.
	*/
	
//--------------------------------------------------------	
	
	NVIC_GLOBAL_ENABLE();
	
//--------------------------------------------------------		

	/*
		Add application initialization code here.
	*/	
	
	App_init();
	
	printf("Gevico-Nano-STC8A Core Bord \r\n version:LA \r\n");
}

/*--------------------------------------------------------
| @Description: MCU SYSCLK peripheral Configure function |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void STC8Ax_SYSCLK_Config(void)
{
	SYSCLK_InitType SYSCLK_InitStruct; /* Declare structure */
	SYSCLK_InitStruct.MCLKSrc = AUTO;
	SYSCLK_InitStruct.SCLKDiv = 0; /* if SCLKDiv = 0, Not output */
	SYSCLK_InitStruct.SCLKOutPin = SCLK_OUT_P16;
	SYSCLK_Init(&SYSCLK_InitStruct);
}

/*--------------------------------------------------------
| @Description: MCU GPIO peripheral Configure function   |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void STC8Ax_GPIO_Config(void)
{

	P0_MODE_WEAK_PULL(Pin_All);
	P1_MODE_WEAK_PULL(Pin_All);
	P2_MODE_WEAK_PULL(Pin_All);
	P3_MODE_WEAK_PULL(Pin_All);
	P4_MODE_WEAK_PULL(Pin_All);
	P5_MODE_WEAK_PULL(Pin_All);
	P54_RST_ENABLE();
	
    /* Run lamp */
	P5_MODE_OUT_PP(Pin_0);  //P50 
	
}

/*--------------------------------------------------------
| @Description: MCU EXTI peripheral Configure function   |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void STC8Ax_EXTI_Config(void)
{
	EXTI0_TRIMode_Init(EXTI_Tri_Falling);
	NVIC_EXTI0_Init(NVIC_PR0,ENABLE);
}

/*--------------------------------------------------------
| @Description: MCU TIMER peripheral Configure function  |
| @param      : None                                     |
| @return     : None                                     |
| @note       : Timer time unit: us                      |
--------------------------------------------------------*/

void STC8Ax_TIMER_Config(void)
{
	TIMER_InitType TIMER_InitStruct;
	
	TIMER_InitStruct.Type = TIMER_Type_Timer;
	TIMER_InitStruct.Mode = TIMER_16BitAutoReload;
	TIMER_InitStruct.TCycle = TIMER_TCY_1T;
	TIMER_InitStruct.ClkOut = DISABLE;
	TIMER_InitStruct.Value = 1000;     //1ms
	TIMER_InitStruct.Run = ENABLE;
	TIMER0_Init(&TIMER_InitStruct);
	NVIC_TIMER0_Init(NVIC_PR0,ENABLE);

}

/*--------------------------------------------------------
| @Description: MCU UART peripheral Configure function   |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void STC8Ax_UART_Config(void)
{
	UART_InitType UART_InitStruct;	

	/* UART1 TXD */
	P3_MODE_OUT_PP(Pin_1);
	/* UART1 RXD */
	P3_MODE_IN_FLOATING(Pin_0); 

	UART_InitStruct.Mode = UART_8bit_BRTx;
	UART_InitStruct.BRTGen = UART_BRT_TIM1;
	UART_InitStruct.BRTMode = UART_BRT_1T;
	UART_InitStruct.BaudRate = 115200;
	UART_InitStruct.RxEnable = ENABLE;
	UART1_Init(&UART_InitStruct);
	NVIC_UART1_Init(NVIC_PR3,ENABLE);

}

/*--------------------------------------------------------
| @Description: MCU COMP peripheral Configure function   |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void STC8Ax_COMP_Config(void)
{
	COMP_InitType COMP_InitStruct;
	
	P3_MODE_IN_FLOATING(Pin_7);
	COMP_InitStruct.PEPin = COMP_PE_P37;
	COMP_InitStruct.NEPin = COMP_IN_REV;
	COMP_InitStruct.AC_FTW = ENABLE;
	COMP_InitStruct.DC_FTW = 0x08;
	COMP_InitStruct.DIRPut = COMP_DIR_Forward;
	COMP_InitStruct.Output = ENABLE;
	COMP_InitStruct.Run = ENABLE;
	COMP_Init(&COMP_InitStruct);
	NVIC_COMP_Init(NVIC_PR0,COMP_Tri_Edge);
}

/*--------------------------------------------------------
| @Description: MCU ADC peripheral Configure function    |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void STC8Ax_ADC_Config(void)
{
	ADC_InitType ADC_InitStruct;

	P1_MODE_IN_FLOATING(Pin_3);
	ADC_InitStruct.Power = ENABLE;
	ADC_InitStruct.Road = ADC_Road_P13;
	ADC_InitStruct.Speed = 0x0F;
	ADC_InitStruct.Align = ADC_Right;
	ADC_InitStruct.Run = ENABLE;
	ADC_Init(&ADC_InitStruct);
	NVIC_ADC_Init(NVIC_PR1,ENABLE);
}

/*--------------------------------------------------------
| @Description: MCU PCA peripheral Configure function    |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void STC8Ax_PCA_Config(void)
{
	PCA0_TIM_Init(1000);
	NVIC_PCA0_TIM_POP_Init(ENABLE);

	PCA_CNT_Init(PCA_SCLK_DIV_1,0,ENABLE);
	NVIC_PCA_CNT_Init(NVIC_PR1,DISABLE);

	// PCA0_PWM_Init(PCA_PWM_10Bit,0xf5);
	// NVIC_PCA3_PWM_CAP_Init(PCA_Tri_Falling);
    // GPIO_PCA_SWPort(SW_PORT4);

}

/*--------------------------------------------------------
| @Description: MCU PWM peripheral Configure function    |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void STC8Ax_PWM_Config(void)
{
	PWM0_Init(PWM_Start_Low_Level,0x0000,0x0020,ENABLE);
	PWM_CNT_Init(PWM_SCLK_DIV_1,0x1000,ENABLE);

	// NVIC_PWM0_Init(PWM_Flip_None);
	// NVIC_PWM_CNT_Init(NVIC_PR1,DISABLE);

}

/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/



